//
//  ESWeChatNetTask.m
//  EasyStyle
//
//  Created by FK on 15/12/29.
//  Copyright © 2015年 FK. All rights reserved.
//

#import "ESWeChatNetTask.h"
#import "ESNetRequestWork.h"
#import "ESWeixinNetResult.h"
#import "ESWeChatDAO.h"
#import "ESSpotlightSeacrchManager.h"

@implementation ESWeChatNetTask

- (void)startNetRequestTaskWithPageNum:(NSNumber *)pageNum isHeaderUpdate:(BOOL)headerUpdate
{
    NSDictionary *paramDict = @{@"key":WeixinSelected_AppKey,@"pno":pageNum};
    
    [ESNetRequestWork NetRequestGETWithRequestURL:WeixinSelected_URL
                                    WithParameter:paramDict
                             WithReturnValeuBlock:^(id returnValue) {
                                 [self fetchDataSuccessWithResultData:returnValue isHeaderUpdate:headerUpdate];
                             } WithFailureBlock:^{
                                 [self netFailure];
                             }];
    
    
}

#pragma 获取到正确的数据，对正确的数据进行处理
- (void)fetchDataSuccessWithResultData:(NSData *)resultData isHeaderUpdate:(BOOL)headerUpdate
{
    ESWeixinNetResult *netResult = [ESWeixinNetResult new];
    
    if ([netResult respondsToSelector:@selector(parseNetResult:)]) {
        
#if DEBUG
        NSString *result  =[[ NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
        NSLog(@"%@", result);
#endif
        NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:resultData options:NSJSONReadingAllowFragments error:nil];
        [netResult parseNetResult:jsonDictionary];
        
        if ([netResult.errorCode intValue] == 0) {
            
            [[ESDataController shareInstance]saveWeChatNewsPageNum:netResult.pno];
            
            NSMutableArray *reArray = [NSMutableArray arrayWithCapacity:netResult.newsList.count];
            if (netResult.newsList.count > 0) {
                for (NSDictionary *dict in netResult.newsList) {
                    WxNewsObject *object = [WxNewsObject new];
                    object.newsID = dict[@"id"];
                    object.title = dict[@"title"];
                    object.source = dict[@"source"];
                    object.firstImg = dict[@"firstImg"];
                    object.mark = dict[@"mark"];
                    object.url = dict[@"url"];
                    [reArray addObject:object];
                }
            }

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                /// 下拉刷新： 清空数据库
                if (headerUpdate) {
                    /// 清除spotlight中可被查询的数据
                    NSMutableArray *array = [[ESWeChatDAO shareInstance]queryAllFromDB];
                    [[ESSpotlightSeacrchManager sharedInstance]removeRecords:array];
                    
                    [[ESWeChatDAO shareInstance]deleteAllFromDB];
                }
                [[ESWeChatDAO shareInstance]insertCityDBWith:reArray];
                
                [[ESSpotlightSeacrchManager sharedInstance]addRecords:reArray];
            });
 
            self.returnBlock(reArray);
            
        } else {
            [self errorCodeWithDic:netResult];
        }
    }
}

#pragma 对ErrorCode进行处理
-(void)errorCodeWithDic: (ESNetResult *) errorDic
{
    self.errorBlock(errorDic);
}

#pragma 对网路异常进行处理
-(void)netFailure
{
    self.failureBlock();
}



@end
